package com.qdxwx.data.service.company;

import com.qdxwx.common.exception.AccountException;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.company.CompanyAccountMapper;
import com.qdxwx.models.company.CompanyAccount;
import com.qdxwx.models.company.CompanyAccountDetail;
import com.qdxwx.models.enums.AccountBizType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class CompanyAccountServiceImpl implements CompanyAccountService {

    @Autowired
    private CompanyAccountMapper companyAccountMapper;
    @Autowired
    private CompanyAccountDetailServiceImpl companyAccountDetailService;

    @Override
    public int insert(CompanyAccount account) {
        account.setUpdateTime(LocalDateTime.now());
        return companyAccountMapper.insert(account);
    }

    @Override
    public CompanyAccount create(int companyId) {
        CompanyAccount companyAccount = new CompanyAccount();
        companyAccount.setAmount(0);
        companyAccount.setFrozenAmount(0);
        companyAccount.setCompanyId(companyId);
        companyAccount.setVersion(1);
        companyAccount.setIsDisabled(0);
        insert(companyAccount);
        return companyAccount;
    }

    @Override
    public int update(CompanyAccount account) {
        if(account.getCompanyId() < 1) return 0;
        account.setUpdateTime(LocalDateTime.now());
        return companyAccountMapper.update(account);
    }

    @Override
    public int deduct(int companyId, int amount, int orderId, String description, AccountBizType accountBizType) throws AccountException {
        if (companyId <= 0 || amount <= 0) {
            return 0;
        }
        CompanyAccount companyAccount = get(companyId);
        if (companyAccount == null) {
            companyAccount = create(companyId);
        }
        companyAccountMapper.deduct(companyId, amount);
        // 添加账户历史操作记录
        CompanyAccountDetail accountDetail = new CompanyAccountDetail();
        accountDetail.setCompanyId(companyId);
        accountDetail.setOrderId(orderId);// 订单id
        accountDetail.setIncome(0);//收入
        accountDetail.setDeduct(amount);//支出
        accountDetail.setBefore(companyAccount.getAmount());
        accountDetail.setResult(companyAccount.getAmount() - amount);
        accountDetail.setFrom(accountBizType.toString());
        accountDetail.setRemark(description);
        accountDetail.setVersion(companyAccount.getVersion() + 1);
        return companyAccountDetailService.insert(accountDetail);
    }

    @Override
    public int income(int companyId, int amount, int orderId, String description, AccountBizType accountBizType) throws AccountException {

        if (companyId <= 0 || amount <= 0) {
            return 0;
        }
        // 每次提成都有orderId，防止重复提成查询判断
        CompanyAccountDetail accountDetail = companyAccountDetailService.getByOrderId(orderId);
        if (accountDetail != null) {
            throw new AccountException("此订单已存在资金提成记录！");
        }
        CompanyAccount companyAccount = get(companyId);
        if (companyAccount == null) {
            companyAccount = create(companyId);
        }
        companyAccountMapper.income(companyId, amount);
        // 添加账户历史操作记录
        accountDetail = new CompanyAccountDetail();
        accountDetail.setCompanyId(companyId);
        accountDetail.setOrderId(orderId);// 订单id
        accountDetail.setIncome(amount);//收入
        accountDetail.setDeduct(0);//支出
        accountDetail.setBefore(companyAccount.getAmount());
        accountDetail.setResult(companyAccount.getAmount() + amount);
        accountDetail.setFrom(accountBizType.toString());
        accountDetail.setRemark(description);
        accountDetail.setVersion(companyAccount.getVersion() + 1);
        return companyAccountDetailService.insert(accountDetail);
    }

    @Override
    public int deleteById(int companyId) {
        if(companyId < 1) return 0;
        return companyAccountMapper.deleteById(companyId);
    }

    @Override
    public CompanyAccount get(int companyId) {
        if(companyId < 1) return null;
        return companyAccountMapper.selectById(companyId);
    }

    @Override
    public List<CompanyAccount> getList(CompanyAccount account, PageRequest page) {
        return companyAccountMapper.selectList(account,page);
    }

    @Override
    public long count(CompanyAccount account) {
        return companyAccountMapper.selectCount(account);
    }

}
